//
//  WNRankVC.swift
//  WalkNovel
//
//  Created by Kevin on 2021/10/29.
//

import UIKit
import JXSegmentedView

class WNRankVC: WNBaseVC {
    
    lazy var listContainerView: JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    let dataSource = JXSegmentedTitleDataSource()
    
    let segmentedView = JXSegmentedView()
        
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        dataSource.titles = ["書籍", "用戶", "禮物"]
        dataSource.titleNormalColor = .init(named: "black_202336")!
        dataSource.titleSelectedColor = .init(named: "red_FE95B4")!
        dataSource.titleNormalFont = .wn_font(name: .bold, size: 14)
        dataSource.titleSelectedFont = .wn_font(name: .bold, size: 15)
        segmentedView.backgroundColor = .white
        segmentedView.delegate = self
        segmentedView.dataSource = dataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 64
        indicator.indicatorHeight = 4
        indicator.indicatorCornerRadius = 2
        indicator.indicatorPosition = .bottom
        indicator.indicatorColor = .init(named: "red_FE95B4")!
        segmentedView.indicators = [indicator]
        
        wn_hidNavView()
        view.addSubview(segmentedView)
        segmentedView.listContainer = listContainerView
        segmentedView.snp.makeConstraints { (make) in
            make.top.equalTo(StatusBarHeight)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: ScreenW, height: 40))
        }
        view.addSubview(listContainerView)
        listContainerView.snp.makeConstraints { (make) in
            make.top.equalTo(segmentedView.snp.bottom)
            make.left.bottom.right.equalTo(0)
        }
    }
}

extension WNRankVC: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

extension WNRankVC: JXSegmentedViewDelegate, JXSegmentedListContainerViewDataSource {
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        navigationController?.interactivePopGestureRecognizer?.isEnabled = segmentedView.selectedIndex == 0
    }
    
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            return titleDataSource.dataSource.count
        }
        return 0
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            let vc = WNRankDetailVC()
            vc.type = "BOOK"
            return vc
        }else if index == 1 {
            let vc = WNRankDetailVC()
            vc.type = "USER"
            return vc
        }else {
            let vc = WNRankGiftVC()
            vc.type = "GIFTED"
            return vc
        }
    }
}
